/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.SearchKey;
import manager.BookManager;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class SearchController extends SimpleFormController {

    private BookManager bookManager;

    public SearchController() {
        setCommandClass(SearchKey.class);
        setCommandName("searchForm");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {
        SearchKey key = new SearchKey();
        key.setOption("2");
        return key;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        SearchKey key = (SearchKey) command;
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("books", this.bookManager.searchBooks(key.getKey(), key.getOption()));
        return new ModelAndView("searchResult", "model", data);
    }

    public void setBookManager(BookManager bookManager) {
        this.bookManager = bookManager;
    }
}